<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>The source code</title>
  <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  <style type="text/css">
    .highlight { display: block; background-color: #ddd; }
  </style>
  <script type="text/javascript">
    function highlight() {
      document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
    }
  </script>
</head>
<body onload="prettyPrint(); highlight();">
  <pre class="prettyprint lang-js"><span id='global-property-'>/**
</span> * @ignore
 * contextmenu for kissy editor
 * @author yiminghe@gmail.com
 */
KISSY.add(&quot;editor/plugin/contextmenu&quot;, function (S, Editor, Menu, focusFix) {

    Editor.prototype.addContextMenu = function (id, filter, cfg) {

        var self = this;

        cfg = cfg || {};

        var event = cfg.event;
        if (event) {
            delete  cfg.event;
        }

        cfg.prefixCls = self.get('prefixCls') + &quot;editor-&quot;;
        cfg.editor = self;
        cfg.focusable = 1;
        cfg.zIndex = Editor.baseZIndex(Editor.ZIndexManager.POPUP_MENU);

        var menu = new Menu.PopupMenu(cfg);

        focusFix.init(menu);

        menu.on(&quot;afterRenderUI&quot;, function () {
            menu.get(&quot;el&quot;).on(&quot;keydown&quot;, function (e) {
                if (e.keyCode == S.Event.KeyCode.ESC) {
                    menu.hide();
                }
            });
        });

        self.docReady(function () {
            var doc = self.get(&quot;document&quot;);
            // 编辑器获得焦点，不会触发 menu el blur？
            doc.on(&quot;mousedown&quot;, function (e) {
                if (e.which == 1) {
                    menu.hide();
                }
            });
            doc.delegate(&quot;contextmenu&quot;, filter, function (ev) {
                ev.halt();
                showNow(ev);
            });
        });

        function showNow(ev) {
            var t = S.all(ev.target);

            // ie 右键作用中，不会发生焦点转移，光标移动
            // 只能右键作用完后才能，才会发生光标移动,range变化
            // 异步右键操作
            // qc #3764,#3767
            var x = ev.pageX,
                y = ev.pageY;
            if (!x) {
                return;
            } else {
                var translate = Editor.Utils.getXY({
                    left: x,
                    top: y
                }, self);
                x = translate.left;
                y = translate.top;
            }
            setTimeout(function () {
                menu.set(&quot;editorSelectedEl&quot;, t, {
                    silent: 1
                });
                menu.move(x, y);
                self.fire(&quot;contextmenu&quot;, {
                    contextmenu: menu
                });
                menu.show();
                window.focus();
                document.body.focus();
                // 防止焦点一直在 el，focus 无效
                menu.focus();
            }, 30);
        }

        if (event) {
            showNow(event);
        }

        self.addControl(id + &quot;/contextmenu&quot;, menu);

        return menu;
    };
}, {
    requires: ['editor', 'menu', './focus-fix']
});
</pre>
</body>
</html>
